package com.manli.api.util;

import java.util.List;
 
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
 
public class XmlTool {
	/**
     * String 转 org.dom4j.Document
     * @param xml
     * @return
     * @throws DocumentException
     */
    public static Document strToDocument(String xml){
        try {
        	//加上xml标签是为了获取最外层的标签，如果不需要可以去掉
			return DocumentHelper.parseText("<xml>"+xml+"</xml>");
		} catch (DocumentException e) {
			return null;
		}
    }
 
    /**
     * org.dom4j.Document 转  com.alibaba.fastjson.JSONObject
     * @param xml
     * @return
     * @throws DocumentException
     */
    public static JSONObject documentToJSONObject(String xml){
		return elementToJSONObject(strToDocument(xml).getRootElement());
    }
 
    /**
     * org.dom4j.Element 转  com.alibaba.fastjson.JSONObject
     * @param node
     * @return
     */
    public static JSONObject elementToJSONObject(Element node) {
        JSONObject result = new JSONObject();
        // 当前节点的名称、文本内容和属性
        List<Attribute> listAttr = node.attributes();// 当前节点的所有属性的list
        for (Attribute attr : listAttr) {// 遍历当前节点的所有属性
            result.put(attr.getName(), attr.getValue());
        }
        // 递归遍历当前节点所有的子节点
        List<Element> listElement = node.elements();// 所有一级子节点的list
        if (!listElement.isEmpty()) {
            for (Element e : listElement) {// 遍历所有一级子节点
                if (e.attributes().isEmpty() && e.elements().isEmpty()) // 判断一级节点是否有属性和子节点
                    result.put(e.getName(), e.getTextTrim());// 沒有则将当前节点作为上级节点的属性对待
                else {
                    if (!result.containsKey(e.getName())) // 判断父节点是否存在该一级节点名称的属性
                        result.put(e.getName(), new JSONArray());// 没有则创建
                    ((JSONArray) result.get(e.getName())).add(elementToJSONObject(e));// 将该一级节点放入该节点名称的属性对应的值中
                }
            }
        }
        return result;
    }
    
    public static void main(String[] args) {
		String string ="<msg>\n" +
                "\t<appmsg appid=\"wx77d53b84434b9d9a\" sdkver=\"0\">\n" +
                "\t\t<title>13.22元 抖音网红韩国懒人挂脖风扇户外旅游风扇学生随身风扇运动USB风扇 拼多多</title>\n" +
                "\t\t<des>【618狂欢热卖】全新升级【豪华版】【带香薰,无刷超静音马达,大功率风力,变换七彩灯,硅胶材质】,超长续航电池,解放你的双手。手持,挂脖子,台式等多功能场景使用,本店质量过硬,工厂直销。【七天无理由退换】【十五天换新】【一年保修】【100%质量保证。让你售后无忧】 强烈推荐豪华版长续航。</des>\n" +
                "\t\t<action />\n" +
                "\t\t<type>5</type>\n" +
                "\t\t<showtype>0</showtype>\n" +
                "\t\t<soundtype>0</soundtype>\n" +
                "\t\t<mediatagname />\n" +
                "\t\t<messageext />\n" +
                "\t\t<messageaction />\n" +
                "\t\t<content />\n" +
                "\t\t<contentattr>0</contentattr>\n" +
                "\t\t<url>https://mobile.yangkeduo.com/goods.html?_wvx=10&amp;refer_share_uid=8703300241&amp;share_uin=U37PQBESC3FRNUSL2IBI34EINI_GEXDA&amp;page_from=23&amp;_wv=41729&amp;refer_share_channel=message&amp;refer_share_id=AEFLoXQEBY3gljEieuC50JBCQjE1lwCx&amp;share_uid=8703300241&amp;goods_id=9288041855</url>\n" +
                "\t\t<lowurl />\n" +
                "\t\t<dataurl />\n" +
                "\t\t<lowdataurl />\n" +
                "\t\t<songalbumurl />\n" +
                "\t\t<songlyric />\n" +
                "\t\t<appattach>\n" +
                "\t\t\t<totallen>0</totallen>\n" +
                "\t\t\t<attachid />\n" +
                "\t\t\t<emoticonmd5></emoticonmd5>\n" +
                "\t\t\t<fileext />\n" +
                "\t\t\t<cdnthumburl>30580201000451304f0201000204488119ea02032f56c1020448e5e77302045eedb001042a777875706c6f61645f777869645f726235376a75776b6536656b32313139365f313539323633353339330204010400030201000400</cdnthumburl>\n" +
                "\t\t\t<cdnthumbmd5>574d0740aa808de1645c915ea8cdc71f</cdnthumbmd5>\n" +
                "\t\t\t<cdnthumblength>18808</cdnthumblength>\n" +
                "\t\t\t<cdnthumbwidth>200</cdnthumbwidth>\n" +
                "\t\t\t<cdnthumbheight>200</cdnthumbheight>\n" +
                "\t\t\t<cdnthumbaeskey>9539487ce51c3b72eb5ab1f75493e4b4</cdnthumbaeskey>\n" +
                "\t\t\t<aeskey>9539487ce51c3b72eb5ab1f75493e4b4</aeskey>\n" +
                "\t\t\t<encryver>0</encryver>\n" +
                "\t\t\t<filekey>wxid_rb57juwke6ek21197_1592643823</filekey>\n" +
                "\t\t</appattach>\n" +
                "\t\t<extinfo />\n" +
                "\t\t<sourceusername />\n" +
                "\t\t<sourcedisplayname />\n" +
                "\t\t<thumburl />\n" +
                "\t\t<md5 />\n" +
                "\t\t<statextstr>GhQKEnd4NzdkNTNiODQ0MzRiOWQ5YQ==</statextstr>\n" +
                "\t\t<directshare>0</directshare>\n" +
                "\t</appmsg>\n" +
                "\t<fromusername>wxid_o4ciksvii60k12</fromusername>\n" +
                "\t<scene>0</scene>\n" +
                "\t<appinfo>\n" +
                "\t\t<version>9</version>\n" +
                "\t\t<appname>拼多多</appname>\n" +
                "\t</appinfo>\n" +
                "\t<commenturl />\n" +
                "</msg>";
        JSONObject xml =  XmlTool.documentToJSONObject(string);

		System.out.println(xml);
	}
}